package com.ytxg.health_management.healthModel.service;

import com.ytxg.health_management.healthModel.entity.HealthModel;
import com.ytxg.health_management.healthModel.entity.HealthModelRecord;
import com.ytxg.health_management.healthModel.mapper.ModelMapper;
import com.ytxg.health_management.healthModel.mapper.ModelRecordMapper;
import com.ytxg.health_management.pojo.entity.Result;
import com.ytxg.health_management.util.MyException;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ModelServiceImpl implements ModelService {
    @Resource
    private ModelMapper modelMapper;
    @Resource
    private ModelRecordMapper modelRecordMapper;

    /**
     * 新增模型
     */
    @Override
    public Result<String> addModel(HealthModel healthModel, String name) {
        healthModel.setCreateTime(new Date()).setCreateBy(name).setModelType("1");
        try {
            modelMapper.insertModel(healthModel);
            return Result.success("新建模型成功", null);
        } catch (Exception e) {
            throw new MyException("新建模型失败" + e);
        }
    }
    /**
     * 新增模型
     */
    @Override
    public Result<String> addModelAdmin(HealthModel healthModel) {
        healthModel.setCreateTime(new Date()).setCreateBy("管理员").setModelType("0");
        try {
            modelMapper.insertModel(healthModel);
            return Result.success("新建模型成功", null);
        } catch (Exception e) {
            throw new MyException("新建模型失败" + e);
        }
    }

    /**
     * 新增模型记录
     */
    @Override
    public Result<String> addModelRecord(HealthModelRecord healthModelRecord, String name) {
        healthModelRecord.setCreateBy(name);
        if(healthModelRecord.getCreateTime()==null){
            healthModelRecord.setCreateTime(new Date());
        }
        List<HealthModel> healthModels = modelMapper.selectModel(new HealthModel().setId(healthModelRecord.getModelId()));
        if (healthModels.isEmpty()) {
            throw new MyException("模型不存在");
        }

        Double value = Double.parseDouble(healthModelRecord.getValue());
        Double min = Double.parseDouble(healthModels.get(0).getMinValue());
        Double max = Double.parseDouble(healthModels.get(0).getMaxValue());
        if (value < min || value > max) {
            healthModelRecord.setStatus("异常");
        } else {
            healthModelRecord.setStatus("正常");
        }
        try {
            modelRecordMapper.insertModelRecord(healthModelRecord);
            return Result.success("新增模型记录成功", null);
        } catch (Exception e) {
            throw new MyException("新增模型记录失败" + e);
        }

    }

    /**
     * 删除模型
     */
    @Override
    @Transactional
    public Result<String> deleteModel(int id) {
        try {
            modelRecordMapper.deleteModelRecord(new HealthModelRecord().setModelId(id));
            modelMapper.deleteModel(new HealthModel().setId(id));
            return Result.success("删除模型成功", null);
        } catch (Exception e) {
            throw new MyException("删除模型失败" + e);
        }
    }

    @Override
    public Result<String> deleteModelRecord(int id) {
        try {
            modelRecordMapper.deleteModelRecord(new HealthModelRecord().setId(id));
            return Result.success("删除模型记录成功", null);
        } catch (Exception e) {
            throw new MyException("删除模型记录失败" + e);
        }

    }

    /**
     * 查询模型
     */
    @Override
    public Result<List<HealthModel>> selectModel(HealthModel healthModel) {
        List<HealthModel> healthModels = modelMapper.selectModel(healthModel);
        return Result.success(healthModels);
    }
    /**
     * 查询模型管理员
     */
    @Override
    public Result<Map<String,Object>> selectModelAll(HealthModel healthModel) {
        healthModel.setCurrentPage((healthModel.getCurrentPage()-1)*healthModel.getPageSize());
        List<HealthModel> healthModels = modelMapper.selectModel(healthModel);
        int total = modelMapper.selectCount(healthModel);
        HashMap<String, Object> res = new HashMap<>();
        res.put("total",total);
        res.put("modelList",healthModels);
        return Result.success(res);
    }

    /**
     * 查询模型记录
     */
    @Override
    public Result<List<HealthModelRecord>> getModelRecord(String createBy) {
        try {
            List<HealthModelRecord> healthModelRecords = modelRecordMapper.selectModelRecord(new HealthModelRecord().setCreateBy(createBy));
            return Result.success(healthModelRecords);
        } catch (Exception e) {
            throw new MyException("查询记录失败" + e);
        }

    }

    /**
     * 获取模型名
     */
    @Override
    public Result<Map<Integer,String>> getModelName(String name) {
        List<HealthModelRecord> records = modelRecordMapper.selectModelName(new HealthModelRecord().setCreateBy(name));
        Map<Integer,String> map = new HashMap<>();
        for (HealthModelRecord record : records) {
            map.put(record.getModelId(),record.getHealthModel().getName());
        }
        return Result.success(map);
    }

    /**
     * 获取图表数据
     */
    @Override
    public Result<ArrayList<Object>> getChartData(int id, String name) {
        List<HealthModelRecord> healthModelRecords = modelRecordMapper.selectModelRecord(new HealthModelRecord().setModelId(id).setCreateBy(name));
        ArrayList<Object> res = new ArrayList<>();
        for (HealthModelRecord healthModelRecord : healthModelRecords) {
            ArrayList<Object> list = new ArrayList<>();

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = dateFormat.format(healthModelRecord.getCreateTime());
            list.add(format);
            list.add(Integer.parseInt(healthModelRecord.getValue()));
            res.add(list);
        }
        return Result.success(res);
    }
}
